/**
 * 
 */
package com.p2p.front.dal.dao;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.base.dao.BaseMapper;
import com.p2p.base.model.Money;
import com.p2p.base.page.PageVo;
import com.p2p.front.dal.model.LoanRepay;

/**
 * @author ruiqi
 *
 */
public interface LoanRepayDao extends BaseMapper{

	public String all_columns="rid,lid,slid,samount,uid,no,start_time,time,repay_amount,principal,return_amount,return_principal,loan_fee,late_fee,early,picurl,lateurl,status,ostatus,uptime,paytime,oktime";
	
	void create(LoanRepay loanRepay);
	
	
	int update(LoanRepay loanRepay);
	
	@Select("select "+all_columns+" from p2p_loan_repay where lid=#{loanId}")
	@ResultMap(value="LoanRepayResultMap")
	List<LoanRepay> queryByLoanId(@Param(value="loanId") Long loanId);
	
	@Select("select "+all_columns+" from p2p_loan_repay where lid=#{loanId} and status=#{status} order by no asc")
	@ResultMap(value="LoanRepayResultMap")
	List<LoanRepay> queryByLoanIdAndStatus(@Param(value="loanId") Long loanId,@Param(value="status") Integer status);
	
	@Select("select "+all_columns+" from p2p_loan_repay where lid=#{loanId} and no=#{no}")
	@ResultMap(value="LoanRepayResultMap")
	LoanRepay getByLoanIdAndNo(@Param(value="loanId") Long loanId,@Param(value="no")Integer no);
	
	@Select("select "+all_columns+" from p2p_loan_repay where rid=#{rid}")
	@ResultMap(value="LoanRepayResultMap")
	LoanRepay getByRid(Long rid);
	
	@Transactional(propagation=Propagation.MANDATORY)
	@Select("select "+all_columns+" from p2p_loan_repay where rid=#{rid} for update")
	@ResultMap(value="LoanRepayResultMap")
	LoanRepay selectForUpdateByRid(Long rid);
	
	@Update("update p2p_loan_repay set return_amount=return_amount+#{returnAmount.cent},return_principal=return_principal+#{returnPrincipal.cent} where lid=#{loanId} and no=#{no}")
	int updateForAddRepay(@Param("loanId") Long loanId,@Param("no")Integer no,@Param("returnAmount")Money returnAmount,@Param("returnPrincipal")Money returnPrincipal);

	@Update("update p2p_loan_repay set status=#{status} where rid=#{rid} and status=#{oldStatus}")
	int updateStatus(@Param("rid")Long rid,@Param("oldStatus")Integer oldStatus,@Param("status")Integer status);
	
	@Update("update p2p_loan_repay set status=#{status} where lid=#{loanId} and no=#{no} and status=#{oldStatus}")
	int updateStatusByLid(@Param("loanId") Long loanId,@Param("no")Integer no,@Param("oldStatus")Integer oldStatus,@Param("status")Integer status);
	
	@Update("update p2p_loan_repay set status=#{status},paytime=NOW() where rid=#{rid} and status=#{oldStatus}")
	int updateStatusForReturn(@Param("rid")Long rid,@Param("oldStatus")Integer oldStatus,@Param("status")Integer status);
	
	List<LoanRepay> listPage(@Param("startTime")Date startTime,@Param("endTime")Date endTime,@Param("status")Integer status);

	@Select("select * from p2p_loan_repay where "
			+ " time between #{otherData.begin} and #{otherData.end}")
	List<LoanRepay> queryByTime(PageVo form);

	@Select("select * from p2p_loan_repay where status <21 and "
			+ " time < #{otherData.begin}")
	List<LoanRepay> queryExceedRepayByTime(PageVo form);
	
	//@Select("select "+all_columns+" from p2p_loan_repay where status =#{status} and time < #{endTime} and time > #{startTime}")
	List<LoanRepay> queryByStatusAndReturnTime(@Param("status")Integer status,@Param("startTime")Date startTime,@Param("endTime")Date endTime);
	
	List<LoanRepay> queryByStatusAndHasExpire(@Param("status")Integer status);
}
